package com.jbit.dm212.service.impl;

import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jbit.dm212.entity.dto.DmItemTypeDTO;
import com.jbit.dm212.entity.dto.FloorDTO;
import com.jbit.dm212.entity.po.DmItem;
import com.jbit.dm212.entity.po.DmItemType;
import com.jbit.dm212.mapper.DmItemTypeMapper;
import com.jbit.dm212.service.DmItemService;
import com.jbit.dm212.service.DmItemTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class DmItemTypeServiceImpl implements DmItemTypeService {
    @Resource
    private DmItemTypeMapper dmItemTypeMapper;
    @Resource
    private DmItemService dmItemService;
    @Override
    public List<DmItemTypeDTO> getAllType() {
        //泛型是我们的主要查询表
        MPJLambdaWrapper<DmItemType> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(DmItemType::getId,
                DmItemType::getItemType,
                DmItemType::getAliasName,
                DmItemType::getLevel,
                DmItemType::getParent);
        lambdaWrapper.eq(DmItemType::getLevel,1);
        //collection 使我们的关联的表
        lambdaWrapper.selectCollection(DmItemType.class,DmItemTypeDTO::getChildren,map->{
            return map.id(DmItemType::getId)
                    .result(DmItemType::getItemType)
                    .result(DmItemType::getAliasName)
                    .result(DmItemType::getLevel)
                    .result(DmItemType::getParent);
        });
        lambdaWrapper.leftJoin(DmItemType.class,DmItemType::getParent,DmItemType::getId);

        List<DmItemTypeDTO> list = dmItemTypeMapper.selectJoinList(DmItemTypeDTO.class,lambdaWrapper);
        list.forEach(dto -> dto.setHotItems(dmItemService.getHotItemsByTypeId(dto.getId())));

        return list;
    }

    @Override
    public List<DmItemTypeDTO> getTransverse() {
        MPJLambdaWrapper<DmItemType> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(DmItemType::getId,
                DmItemType::getItemType,
                DmItemType::getLevel,
                DmItemType::getAliasName,
                DmItemType::getKey);
        lambdaWrapper.eq(DmItemType::getLevel,1);
        //返回结果不再是resultType,而是resultMap了
        return dmItemTypeMapper.selectJoinList(DmItemTypeDTO.class,lambdaWrapper);
    }

    @Override
    public List<FloorDTO> getFloorItems() {
        MPJLambdaWrapper<DmItemType> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.selectAs(DmItemType::getId,"itemTypeId");
        lambdaWrapper.selectAs(DmItemType::getAliasName,"itemTypeName");
        lambdaWrapper.selectAs(DmItemType::getKey,"floor");
        lambdaWrapper.eq(DmItemType::getLevel,1);
        List<FloorDTO> floorDTOList = dmItemTypeMapper.selectJoinList(FloorDTO.class,lambdaWrapper);
        floorDTOList.forEach(floor->floor.setItems(dmItemService.getFloorItems(floor.getItemTypeId())));

        return floorDTOList;
    }

    @Override
    public List<DmItemTypeDTO> getItemTypeByParent(Integer parent) {
        MPJLambdaWrapper<DmItemType> lambdaWrapper = JoinWrappers.lambda();
        lambdaWrapper.eq(DmItemType::getParent,parent);
        return dmItemTypeMapper.selectJoinList(DmItemTypeDTO.class,lambdaWrapper);
    }


}
